﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraEditors.Popup;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraTreeList.Nodes;
using System.ComponentModel;

namespace SAF.Controls
{
    [ToolboxItem(false)]
    [EditorBrowsable(EditorBrowsableState.Never)]
    public class PopupTreeLookUpEditForm : CustomBlobPopupForm
    {
        public PopupTreeLookUpEditForm(TreeLookUpEdit ownerEdit)
            : base(ownerEdit)
        {
            this.SetResultValue(OwnerEdit.EditValue);
            base.ViewInfo.ShowSizeBar = Properties.ShowFooter;
            var lookUpPop = Properties.LookUpPopup;
            base.Controls.Add(lookUpPop);
            Size size = this.DefaultBlobFormSize;
            LookUpPopup.Location = new Point(1, 1);
            lookUpPop.Size = new Size(size.Width - 20, size.Height + 45);
            lookUpPop.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
        }

        protected override object QueryResultValue()
        {
            return this.Properties.Tree.FocusedNode.GetValue(this.Properties.ValueMember);
        }

        private object resultValue;

        protected void SetResultValue(object value)
        {
            this.resultValue = value;
            base.Invalidate();
        }

        protected override Size DefaultBlobFormSize
        {
            get
            {
                Size res = OwnerEdit.Properties.GetDesiredPopupFormSizeCore(false);
                if (res.Width == 0) res.Width = DefaultEmptySize.Width;
                if (res.Height == 0) res.Height = DefaultEmptySize.Height;
                int bestWidth = CalcBestFit(res);
                if (bestWidth != 0) res.Width = bestWidth;
                return res;
            }
        }
        protected virtual int CalcBestFit(Size res)
        {
            return 0;
        }

        protected override Size DefaultEmptySize { get { return new Size(300, 200); } }

        protected TreeLookUpEditPopup LookUpPopup { get { return Properties.LookUpPopup; } }

        public new RepositoryItemTreeLookUpEdit Properties
        {
            get
            {
                return OwnerEdit.Properties as RepositoryItemTreeLookUpEdit;
            }
        }

        public new TreeLookUpEdit OwnerEdit
        {
            get
            {
                return base.OwnerEdit as TreeLookUpEdit;
            }
        }

        protected virtual Size UpdateFormSize(Size sz)
        {
            Size minFormSize = this.MinFormSize;
            return new Size(Math.Max(sz.Width, minFormSize.Width), sz.Height);
        }

        internal void UpdateFocusedNode(TreeListNode node)
        {
            this.Properties.Tree.FocusedNode = node;
        }

        internal void ClearClick()
        {
            this.Properties.ClearClick();
        }
    }
}
